package com.lantosec.hmrp.modules.sysdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.sysdata.entity.SysDataProcess;
import com.lantosec.hmrp.modules.sysdata.entity.SysDataProcessTask;
import com.lantosec.hmrp.modules.sysdata.mapper.SysDataProcessTaskMapper;
import com.lantosec.hmrp.modules.sysdata.service.ISysDataProcessTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.workflow.vo.FlowTaskVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统数据流程任务关联表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-10
 */
@Service
@Primary
@Log4j2
public class SysDataProcessTaskServiceImpl extends ServiceImpl<SysDataProcessTaskMapper, SysDataProcessTask> implements ISysDataProcessTaskService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveTask(Task task, String assignee, String formLayoutId, Integer auditStatus, String comment, String sign) {
        SysDataProcessTask dbDataProcessTask = baseMapper.selectOne(new QueryWrapper<SysDataProcessTask>()
                .eq(String.valueOf(SysDataProcessTask.ColName.pro_ins_id), task.getProcessInstanceId())
                .eq(String.valueOf(SysDataProcessTask.ColName.task_id), task.getId()));
        int flag = 0;
        if (Objects.isNull(dbDataProcessTask)) {
            flag = insertTask(task, assignee, formLayoutId);
        } else {
            dbDataProcessTask.setTaskAssignee(task.getAssignee());
            dbDataProcessTask.setTaskAuditStatus(auditStatus);
            dbDataProcessTask.setTaskComment(comment);
            dbDataProcessTask.setTaskSign(sign);
            dbDataProcessTask.setFinishTime(new Date());
            flag = baseMapper.updateById(dbDataProcessTask);
        }
        return flag > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTask(Task task, String assignee, String formLayoutId) {
        SysDataProcessTask dataProcessTask = new SysDataProcessTask();
        dataProcessTask.setDataProcessTaskId(UUIDUtils.get());
        dataProcessTask.setProInsId(task.getProcessInstanceId());
        dataProcessTask.setTaskId(task.getId());
        dataProcessTask.setTaskName(StringUtils.isNotEmpty(task.getName()) ? task.getName() : "");
        dataProcessTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
        dataProcessTask.setTaskAssignee(assignee);
        dataProcessTask.setFormLayoutId(formLayoutId);
        dataProcessTask.setCreateTime(new Date());
        return baseMapper.insert(dataProcessTask);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTask(String processInstanceId, String taskId, String assignee, String formLayoutId, Boolean isStart) {
        Date now = new Date();
        SysDataProcessTask dataProcessTask = new SysDataProcessTask();
        dataProcessTask.setDataProcessTaskId(UUIDUtils.get());
        dataProcessTask.setProInsId(processInstanceId);
        dataProcessTask.setTaskId(StringUtils.isEmpty(taskId) ? "" : taskId);
        dataProcessTask.setTaskName(isStart ? "流程开始" : "流程结束");
        dataProcessTask.setTaskAssignee(assignee);
        dataProcessTask.setFormLayoutId(formLayoutId);
        dataProcessTask.setCreateTime(now);
        dataProcessTask.setFinishTime(now);
        return baseMapper.insert(dataProcessTask);
    }

    @Override
    public boolean updateLookTime(String userId, String taskId) {
        SysDataProcessTask dataProcessTask = new SysDataProcessTask();
        dataProcessTask.setLookTime(new Date());
        int flag = baseMapper.update(dataProcessTask, new QueryWrapper<SysDataProcessTask>()
                .eq(String.valueOf(SysDataProcessTask.ColName.task_assignee), userId)
                .eq(String.valueOf(SysDataProcessTask.ColName.task_id), taskId)
                .isNull(String.valueOf(SysDataProcessTask.ColName.look_time))
        );
        return flag > 0;
    }

    @Override
    public List<SysDataProcessTask> listByProInsId(String proInsId) {
        return baseMapper.selectList(new QueryWrapper<SysDataProcessTask>()
                .eq(String.valueOf(SysDataProcessTask.ColName.pro_ins_id), proInsId)
                .orderByDesc(String.valueOf(SysDataProcessTask.ColName.create_time))
                .orderByAsc(String.valueOf(SysDataProcessTask.ColName.finish_time))
        );
    }

    @Override
    public Page<FlowTaskVO> queryPartakeTask(PageVO page, @Param("taskAssignee") String taskAssignee){
        return baseMapper.queryPartakeTask(page,taskAssignee);
    }

    @Override
    public long countPartakeTask(String taskAssignee) {
        return baseMapper.countPartakeTask(taskAssignee);
    }
}
